package org.mall.modules.message.service.impl;

import org.mall.common.api.vo.CommonResult;
import org.mall.common.util.GeneratorSnowFlakeIDUtils;
import org.mall.common.util.ObjectUtils;
import org.mall.convert.system.AppMessageConvert;
import org.mall.modules.member.service.IMemberService;
import org.mall.modules.message.entity.AppMessage;
import org.mall.modules.message.entity.AppNotice;
import org.mall.modules.message.mapper.AppMessageMapper;
import org.mall.modules.message.mapper.AppNoticeMapper;
import org.mall.modules.message.service.AppMessageService;
import org.mall.modules.pojo.common.AppMessageDTO;
import org.mall.modules.pojo.common.AppNoticeDTO;
import org.mall.modules.seller.service.ISellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

/***
 *@author yinxucun
 *@date 2019-12-03 16:59 
 */
@Service
public class AppMessageImpl implements AppMessageService {

    //TODO 需要加上短信编码，以及构造短信内容参数



    @Resource
    AppMessageMapper appMessageMapper;
    @Resource
    AppNoticeMapper appNoticeMapper;
    @Autowired
    private IMemberService memberService;

    @Autowired
    private ISellerService sellerService;

    @Override
    public CommonResult sendAppMessageToUser(AppMessageDTO dto)  {
        if (messageDTOParamsIsNull(dto)){
            return CommonResult.error("参数不足");
        }
        dto.setCreateTime(new Date());
        switch (dto.getType()){
            case 101:
                sendUpdatePasswordAndMobileMessage(dto);
                break;
            case 201:
                 sendPaySuccessMessage(dto);
                 break;
            case 301:
                sendWriterOffOrderMessage(dto);
                break;
            case 107:
                sendWriterMemberNotificationMessage(dto);
                break;
            case 108:
                sendWriterPaymentNoticeMessage(dto);
                break;
            case 109:
                sendWriterOrderNotificationMessage(dto);
                break;
            case 110:
                sendWriterMerchantStoreNoticeMessage(dto);
                break;
            case 111:
                sendWriterVersionNotificationMessage(dto);
                break;
            case 112:
                sendWriterActivityNoticeMessage(dto);
                break;
            case 113:
                sendWriterCommoditynNotificationMessage(dto);
                break;
            case 114:
                sendWriterVerificationCode(dto);
                break;
            case 115:
                sendWriterPromotersAddShopsMessage(dto);
                break;
            case 401:
                sendOrderPaySuccessToSeller(dto);
                break;
            default: break;
        }

        return null;
    }

    @Override
    public CommonResult sendAppMessageToAll(AppNoticeDTO dto)  {
        AppNotice appNotice= ObjectUtils.copyObject(dto, AppNotice.class);

        appNotice.setUpdateTime(new Date());
        appNoticeMapper.insert(appNotice);
        appNoticeMapper.saveLog(appNotice.getId(),dto.getUserId(), LocalDateTime.now());
        return CommonResult.ok();
    }

    /**
     * 发送验证码
     * @param dto
     */
    private void sendWriterVerificationCode(AppMessageDTO dto) {
        // 调用 第三方方法
    }

    /**
     * 商城活动类通知
     * @param dto
     */
    private boolean sendWriterActivityNoticeMessage(AppMessageDTO dto) {
        AppMessage message=convertMessage(dto);
        return  appMessageMapper.insert(message)>0;

    }
    /**
     * 系统版本通知
     * @param dto
     */
    private boolean sendWriterVersionNotificationMessage(AppMessageDTO dto) {
        AppMessage message=convertMessage(dto);
        return  appMessageMapper.insert(message)>0;
    }
    /**
     * 商户店铺消息通知
     * @param dto
     */
    private boolean sendWriterMerchantStoreNoticeMessage(AppMessageDTO dto) {
        AppMessage appMessage = getAppMessage(dto, 2,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    /**
     * 订单类消息通知
     * @param dto
     */
    private boolean sendWriterOrderNotificationMessage(AppMessageDTO dto) {
        AppMessage message=convertMessage(dto);
        return  appMessageMapper.insert(message)>0;
    }

    /**
     * 支付类消息通知
     * @param dto
     */
    private boolean sendWriterPaymentNoticeMessage(AppMessageDTO dto) {
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    /**
     * 会员通知
     * @param dto
     */
    private boolean sendWriterMemberNotificationMessage(AppMessageDTO dto) {
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    /**
     * 商品通知
     * @param dto
     * @return
     */
    private boolean sendWriterCommoditynNotificationMessage(AppMessageDTO dto) {
        AppMessage appMessage=convertMessage(dto);
        return  appMessageMapper.insert(appMessage)>0;
    }

    /**
     * 推广员推广店铺
     * @param dto
     */
    private boolean sendWriterPromotersAddShopsMessage(AppMessageDTO dto) {
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    boolean sendOrderPaySuccessToSeller(AppMessageDTO dto) {
        //通过mq发送短信
        AppMessage appMessage = getAppMessage(dto, 1, "");
        return  appMessageMapper.insert(appMessage)>0;
    }

    boolean sendWriterOffOrderMessage(AppMessageDTO dto) {
        //支付成功
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    boolean sendUpdatePasswordAndMobileMessage(AppMessageDTO dto)  {

        AppMessage message=convertMessage(dto);
        return  appMessageMapper.insert(message)>0;

    }

    boolean sendPaySuccessMessage(AppMessageDTO dto){
        //支付成功
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    private String getMobile(String receiver,Integer type) {
        if (type==1){
            return memberService.getMobileById(receiver);

        }
        if (type==2){
            return sellerService.getMobileById(receiver);
        }
        return null;
    }

    private AppMessage getAppMessage(AppMessageDTO dto, Integer type, String templateCode){
        AppMessage message=convertMessage(dto);

            //TODO 发送短信

        return message;
    }

    boolean messageDTOParamsIsNull(AppMessageDTO dto){
        return  ObjectUtils.isNull(dto.getCreator(),dto.getType(),dto.getReceiver(),dto.getContent());
    }
    void initAppMessage(AppMessage message){
        message.setCreateTime(new Date());
        message.setIsRead(0);
        message.setIsDelete(0);
        message.setId(GeneratorSnowFlakeIDUtils.nextId());
    }

    AppMessage convertMessage(AppMessageDTO dto){
        AppMessage appMessage= AppMessageConvert.INSTANCE.convert(dto);
        initAppMessage(appMessage);
        return appMessage;
    }

}
